﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net;
using System.IO;
using System.Collections;

/// <summary>
///GetTags 的摘要说明
/// </summary>
public class GetTags
{
    public GetTags()
    {
    }
    //POST 提交
    public string GetTag(string title, string cnn)
    {
        string key = "";

        cnn = StripHTMLTest.StripHTML(cnn);
        cnn = cnn.Replace("&", "");
        cnn = cnn.Replace("#", "");
        if (cnn.Length > 900)
        {
            cnn = cnn.Substring(0, 899);
        }
        string url = "http://keyword.discuz.com/related_kw.html?title=" + title + "&content=" + cnn + "&ics=utf-8";
        string conn = Getsite(url);
        if (conn.IndexOf("]]></kw>") > -1)
        {
            string[] KeyList = StringSplit(conn, "]]></kw>");
            foreach (string a in KeyList)
            {

                //a.Substring(a.LastIndexOf("["))+"<br/>"
                if (a.LastIndexOf("[") > -1)
                {
                    key += a.Substring(a.LastIndexOf("[") + 1) + ",";
                }
            }
            key = key.Substring(0, key.Length - 1);
        }
        else
        {
            key = conn;
        }
        return key;

    }
    public string Getsite(string Url)
    {
        string param = "";
        CookieContainer cc = new CookieContainer();
        byte[] bs = Encoding.ASCII.GetBytes(param);
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(Url);
        req.CookieContainer = cc;
        HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
        StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
        string content = reader.ReadToEnd();
        reader.Close();
        myResponse.Close();

        return content;
    }
    public string GetXiangGuanWenZhang(string keyList)
    {
        string str = "";
        if (keyList != "")
        {
             str = "<p>相关文章：<br/><ul>";
            News Addn = new News();
            Hashtable xg = new Hashtable();
            string[] key = keyList.Split(',');
            for (int s = 0; s < key.Length; s++)
            {
                xg = Addn.SelLikeForKey(key[s].ToString(), xg);

            }
            //遍历Hashtable中的键跟值，同时进行关键字替换
            System.Collections.IDictionaryEnumerator enumerator = xg.GetEnumerator();
            int i = 0;
            while (enumerator.MoveNext())
            {
                i++;
                if (i < 9)
                {
                    str += "<li style='width: 350px;display: block;float: left;'><a href='" + enumerator.Key.ToString() + "'>" + enumerator.Value.ToString() + "</a></li>";
                }
            }
            str += "</ul>";
            if (str.Length < 30)
            {
                str = "";
            }
        }
        return str;
    }
    public string GetXiangGuanWenZhang(string keyList, string Nid)
    {
        string str = "";
        if (keyList != "")
        {
            str = "<p>相关文章：<br/><ul>";
            News Addn = new News();
            Hashtable xg = new Hashtable();
            string[] key = keyList.Split(',');
            for (int s = 0; s < key.Length; s++)
            {
                xg = Addn.SelLikeForKey(key[s].ToString(), xg);

            }
            //遍历Hashtable中的键跟值，同时进行关键字替换
            System.Collections.IDictionaryEnumerator enumerator = xg.GetEnumerator();
            int i = 0;
            while (enumerator.MoveNext())
            {
                if (enumerator.Key.ToString() != Nid.ToString())
                {
                    i++;
                    if (i < 9)
                    {
                        str += "<li style='width: 350px;display: block;float: left;'><a href='" + enumerator.Key.ToString() + "'>" + enumerator.Value.ToString() + "</a></li>";
                    }
                }
               
            }
            str += "</ul>";
            if (str.Length < 30)
            {
                str = "";
            }
        }
        //str += "</ul>";
        return str;
    }
    /// <summary>
    /// 将字符串分割成数组
    /// </summary>
    /// <param name="strSource"></param>
    /// <param name="strSplit"></param>
    /// <returns></returns>
    public string[] StringSplit(string strSource, string strSplit)
    {
        string[] strtmp = new string[1];
        int index = strSource.IndexOf(strSplit, 0);
        if (index < 0)
        {
            strtmp[0] = strSource;
            return strtmp;
        }
        else
        {
            strtmp[0] = strSource.Substring(0, index);
            return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
        }
    }
    /// <summary>
    /// 采用递归将字符串分割成数组
    /// </summary>
    /// <param name="strSource"></param>
    /// <param name="strSplit"></param>
    /// <param name="attachArray"></param>
    /// <returns></returns>
    public string[] StringSplit(string strSource, string strSplit, string[] attachArray)
    {
        string[] strtmp = new string[attachArray.Length + 1];
        attachArray.CopyTo(strtmp, 0);

        int index = strSource.IndexOf(strSplit, 0);
        if (index < 0)
        {
            strtmp[attachArray.Length] = strSource;
            return strtmp;
        }
        else
        {
            strtmp[attachArray.Length] = strSource.Substring(0, index);
            return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
        }
    }

}